﻿// like Pv3D's BaseView
// more function to debug, and view object style
/*
* Usage
* setup(autoLoad=true/false) autoLoad init()->update();
* :: setup(false) == autoLoad=false, run init() by hand.
* init() start update() in ENTER_FRAME
* update() override the function want to loop.
* 
* focus(_obj:DisplayObject3D) current camera follow which object.
* */

package com.cf.papervision3d.Utils{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.cf.papervision3d.Utils.setting.*;
	
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.render.BasicRenderEngine;
	
	import com.cf.core.view.Button;
	public class LabUI extends Sprite{
		private var __scene:Scene3D;
		public function set scene(s:Scene3D):void{__scene=s}
		public function get scene():Scene3D{return __scene}
		private var __viewport:Viewport3D;
		public function set viewport(v:Viewport3D):void{__viewport=v}
		public function get viewport():Viewport3D{return __viewport}
		private var __camera:viewMode;
		public function set camera(c:viewMode):void{__camera=c}
		public function get camera():viewMode{return __camera}
		private var __renderer:BasicRenderEngine;
		public function set renderer(r:BasicRenderEngine):void{__renderer=r}
		public function get renderer():BasicRenderEngine{return __renderer}
		private var btn:Button;
		private var __debug:Boolean;
		public function set debug(tf:Boolean):void{__debug=tf}
		public function get debug():Boolean{return __debug}
		
		public function LabUI():void{
			scene = new Scene3D();
			viewport = new Viewport3D();
			camera = new viewMode();
			camera.mode = viewMode.FOCUS;
			renderer = new BasicRenderEngine();
			debug=false;
		}//fn
		
		// setup
		public function setup(_autoLoad:Boolean=true):void{
			// Listen to mouse up and down events on the stage
			stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleMouseWheel);
			addChild(viewport);
			if(_autoLoad){
				init();
			}else{
				btn = new Button();
				btn.value = "Pls Click to load Pv3D";
				btn.x = (stage.stageWidth/2 - btn.width/2);
				btn.y = (stage.stageHeight/2 - btn.height/2);
				addChild(btn);
				btn.addEventListener(MouseEvent.MOUSE_DOWN, handleInit);
			}
			trace("LabUI : setup");
		}//fn
		// init
		public function init():void{
			addEventListener(Event.ENTER_FRAME, handleRenderLoop); // loop
			addEventListener(Event.ENTER_FRAME, handleViewMode); // loop
			trace("LabUI : Inited");
		}//fn
		private function handleInit(e:MouseEvent):void{
			init();
			removeChild(btn);
			btn.removeEventListener(MouseEvent.MOUSE_DOWN, handleInit);
		}//fn
		
		public function update():void{
			// allow override
		}//fn
		
		private function handleRenderLoop(event:Event):void {
			try{
				this.update();
				renderer.renderScene(scene, camera, viewport);
			}catch(e:Error){ trace("LabUI : runtime error \n\t"+ e); }
		}//fn
		// Handler of the Movement of Camera/View
		private function handleMouseDown(e:MouseEvent):void {
			if(debug) camera.onView(e);
		}//fn
		private function handleMouseUp(e:MouseEvent):void {
			if(debug) camera.onStop(e);
		}//fn
		private function handleMouseWheel(e:MouseEvent):void{
			if(debug) camera.onZoom(e);
		}//fn
		private function handleViewMode(e:Event=null):void{
			if(debug) camera.refresh(mouseX,mouseY);
		}//fn
	}//class
}//package